﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Maze_tools;

namespace Maze
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void показатьРекорыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Form2();
            frm.Show();
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void помощьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Form3();
            frm.Show();
        }

        private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Form4();
            frm.Show();
        }

        private void новаяИграToolStripMenuItem_Click(object sender, EventArgs e)
        {
            total1.clear_all();
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            int n = 20, m = 30;
            Random r = new Random();
            maze1.set_size(n, m);
                maze1.init_maze();
                maze1.generate();
                for (int k = 0; k < 300; ++k)
                    maze1.add_more_halls(r.Next(20), r.Next(30));
                maze1.check_maze();

            int d = 17, t=0;

            System.Drawing.Pen myPen;
            myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
            System.Drawing.Graphics formGraphics = this.CreateGraphics();

            Color col = Color.White; 
            formGraphics.Clear(col);

            for (int j = 0; j < m; ++j)
            {
                formGraphics.DrawLine(myPen, 10+d * j, 30, 10+d * (j + 1), 30);
            }
            formGraphics.DrawLine(myPen, 10, 30, 10, 30+d);

            for (int i=0; i<n; ++i)
            {
                formGraphics.DrawLine(myPen, 10, 30+i*d, 10, 30 + (i+1)*d);
                for (int j = 0; j < m; j++)
                {
                    t = maze1.get_cur_val(i,j); ;
                    if ((t & (1 << 0)) != (1 << 0))
                    {
                        formGraphics.DrawLine(myPen, 10 + (j+1) * d, 30+d*i, 10 + (j + 1) * d, 30 + d*(i+1));
                    }
                    if ((t & (1 << 3)) != (1 << 3))
                    {
                        formGraphics.DrawLine(myPen, 10 + j * d, 30 + d * (i + 1), 10 + (j + 1) * d, 30 + d * (i + 1));
                    }
                }
            }

            pictureBox1.Location = new System.Drawing.Point(13 + d * maze1.get_cur_loc().y, 31 + d * maze1.get_cur_loc().x);
            pictureBox2.Location = new System.Drawing.Point(13 + d * maze1.get_finish_loc().y, 31 + d * maze1.get_finish_loc().x);
            pictureBox1.Visible = true;
            pictureBox2.Visible = true;
        }

        private void сохранитьИгруToolStripMenuItem_Click(object sender, EventArgs e)
        {
            maze1.write_maze();
            total1.save_moves();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int tx=pictureBox1.Location.X;
            int ty=pictureBox1.Location.Y;
            if (e.KeyChar == (char)Keys.A || e.KeyChar == char.ToLower((char)Keys.A))
            {
                maze1.move_to(2);
            }
            if (e.KeyChar == (char)Keys.D || e.KeyChar == char.ToLower((char)Keys.D))
            {
                maze1.move_to(0);
            }
            if (e.KeyChar == (char)Keys.W || e.KeyChar == char.ToLower((char)Keys.W))
            {
                maze1.move_to(1);
            }
            if (e.KeyChar == (char)Keys.S || e.KeyChar == char.ToLower((char)Keys.S))
            {
                maze1.move_to(3);
           }
            pictureBox1.Location = new System.Drawing.Point(maze1.get_cur_loc().y*17+13,maze1.get_cur_loc().x*17+31);
            total1.increment();
            if (maze1.win())
            {
                win1.take_score(total1.evaluate(30));
                win1.fwrite_score();
                Form frm = new Form5();
                frm.Show();   
            }
        }

        private void загрузитьИгруToolStripMenuItem_Click(object sender, EventArgs e)
        {
            maze1.read_maze();
            int d = 17, t = 0,n=20,m=30;
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            System.Drawing.Pen myPen;
            myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
            System.Drawing.Graphics formGraphics = this.CreateGraphics();

            Color col = Color.White;
            formGraphics.Clear(col);

            for (int j = 0; j < m; ++j)
            {
                formGraphics.DrawLine(myPen, 10 + d * j, 30, 10 + d * (j + 1), 30);
            }
            formGraphics.DrawLine(myPen, 10, 30, 10, 30 + d);

            for (int i = 0; i < n; ++i)
            {
                formGraphics.DrawLine(myPen, 10, 30 + i * d, 10, 30 + (i + 1) * d);
                for (int j = 0; j < m; j++)
                {
                    t = maze1.get_cur_val(i, j); ;
                    if ((t & (1 << 0)) != (1 << 0))
                    {
                        formGraphics.DrawLine(myPen, 10 + (j + 1) * d, 30 + d * i, 10 + (j + 1) * d, 30 + d * (i + 1));
                    }
                    if ((t & (1 << 3)) != (1 << 3))
                    {
                        formGraphics.DrawLine(myPen, 10 + j * d, 30 + d * (i + 1), 10 + (j + 1) * d, 30 + d * (i + 1));
                    }
                }
            }
            pictureBox1.Location = new System.Drawing.Point(13 + d * maze1.get_cur_loc().y, 31 + d * maze1.get_cur_loc().x);
            pictureBox2.Location = new System.Drawing.Point(13 + d * maze1.get_finish_loc().y, 31 + d * maze1.get_finish_loc().x);
            pictureBox1.Visible = true;
            pictureBox2.Visible = true;
            total1.load_moves();
        }
    }
}
